package com.sptpc.demo.controller;

import com.sptpc.demo.model.Category;
import com.sptpc.demo.model.Drink;
import com.sptpc.demo.service.ICategoryService;
import com.sptpc.demo.service.IDrinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RouterController {
    @Autowired
    public IDrinkService drinkService;

    @Autowired
    public ICategoryService iCategoryService;

    @GetMapping("/")
    public String Login(){
        return "manage/login";
    }

//    后台首页路由
//    get /manage/index -> templates/manage/index.html
    @GetMapping("/manage/index")
    public String index(){
        return "manage/index";
    }

//    后台欢饮页路由
    @GetMapping("/manage/welcome")
    public String welcome(){
        return "manage/welcome";
    }

//    饮品管理首页路由
//    get /menu/drink
    @GetMapping("/menu/drink")
    public String drinkIndex(){
        return "drink/index";
    }

//    添加页面路由
//    get /drink/addform
    @GetMapping("/drink/addform")
    public String drinkAdd(){
        return "drink/add";
    }

//    修改页面路由
//    get /drink/updateform?id=2
    @GetMapping("/drink/updateform")
    public ModelAndView drinkUpdate(@RequestParam Integer id){
        ModelAndView mv = new ModelAndView("drink/edit");
//        TODO：后期从数据库查询
//        Drink build = Drink.builder()
//                .id(2)
//                .name("测试饮品")
//                .price(Money.of(CurrencyUnit.of("CNY"), 10.00))
//                .build();
//        mv.addObject("drink",build);
//        按id查询信息
        Drink drink = drinkService.getDrinkById(id);

        mv.addObject("drink",drink);
        return mv;
}


//分类管理首页路由
    @GetMapping("/menu/category")
    public String categoryIndex(){
        return "category/index";
    }

    //    分类管理新增分类
    @GetMapping("/category/addform")
    public String addNewFrom(){
        return "category/add";
    }

    //    分类管理编辑分类
    @GetMapping("/category/updateform")
    public ModelAndView updateForm(@RequestParam Integer id){
        ModelAndView mv=new ModelAndView("category/edit");
        Category category=iCategoryService.getCategoryById(id);
        mv.addObject("category",category);
        return mv;
    }

}
